package com.op.teachingliteapp.util.http;

import java.util.Map;

import android.content.Context;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.op.teachingliteapp.R;
import com.op.teachingliteapp.util.http.override.StringCacheRequest;

public class VolleySingleton {

	private static final String TAG = VolleySingleton.class.getSimpleName();
	private static VolleySingleton mInstance;
	private static Context mContext;
	private RequestQueue mRequestQueue;
	private ImageLoader mImageLoader;

	private VolleySingleton() {
	}

	/**
	 * 初始化
	 */
	public void init(Context context) {
		mContext = context;
		mRequestQueue = getRequestQueue();
		mImageLoader = new ImageLoader(mRequestQueue, new MemoryCache());
	};

	public static synchronized VolleySingleton getInstance() {
		if (mInstance == null) {
			synchronized (VolleySingleton.class) {
				if (mInstance == null) {
					mInstance = new VolleySingleton();
				}
			}
		}
		return mInstance;
	}

	/**
	 * 得到http请求队列
	 */
	public RequestQueue getRequestQueue() {
		if (mRequestQueue == null) {
			mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
		}
		return mRequestQueue;
	}

	/**
	 * 将请求加入队列
	 */
	public <T> void addToRequestQueue(Request<T> req) {
		getRequestQueue().add(req);
	}

	public ImageLoader getImageLoader() {
		return mImageLoader;
	}

	/**
	 * 加载图片
	 */
	public void loadImage(ImageView imageView, String reqUrl) {
		ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,
				R.drawable.ic_empty, R.drawable.ic_error);
		getImageLoader().get(reqUrl, listener);
	}

	/**
	 * Post请求，加载String，默认不缓存
	 */
	public void load(String reqUrl, final Map<String, String> params,
			Response.Listener<String> listener, Response.ErrorListener errorListener) {
		this.load(reqUrl, params, false, listener, errorListener); // 默认不缓存
	}

	/**
	 * Post请求，加载String，可选择是否缓存
	 */
	public void load(String reqUrl, final Map<String, String> params, boolean isCaching,
			Response.Listener<String> listener, Response.ErrorListener errorListener) {
		Request<String> req = null;
		if (isCaching) {
			req = new StringCacheRequest(Request.Method.POST, reqUrl, listener, errorListener) {
				@Override
				protected Map<String, String> getParams() throws AuthFailureError {
					return params;
				}
			};
		} else {
			req = new StringRequest(Request.Method.POST, reqUrl, listener, errorListener) {
				@Override
				protected Map<String, String> getParams() throws AuthFailureError {
					return params;
				}
			};
		}
		addToRequestQueue(req);
	}

	/**
	 * Get请求，加载String
	 */
	public void load(String reqUrl, Response.Listener<String> listener,
			Response.ErrorListener errorListener) {
		StringRequest req = new StringRequest(Request.Method.GET, reqUrl, listener, errorListener);
		addToRequestQueue(req);
	}

}